<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="物料编码" prop="fmaterialid">
        <el-input
          v-model="queryParams.fmaterialid"
          placeholder="请输入物料编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialname">
        <el-input
          v-model="queryParams.materialname"
          placeholder="请输入物料名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入规格型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="描述" prop="fUnBaseproperty6">
        <el-input
          v-model="queryParams.fUnBaseproperty6"
          placeholder="请输入描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库编码" prop="fstockid">
        <el-input
          v-model="queryParams.fstockid"
          placeholder="请输入仓库编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="stockname">
        <el-input
          v-model="queryParams.stockname"
          placeholder="请输入仓库名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批号" prop="flot">
        <el-input
          v-model="queryParams.flot"
          placeholder="请输入批号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产日期" prop="fproducedate">
        <el-input
          v-model="queryParams.fproducedate"
          placeholder="请输入生产日期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="有效期至" prop="fexpirydate">
        <el-input
          v-model="queryParams.fexpirydate"
          placeholder="请输入有效期至"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存主单位" prop="fbaseqty">
        <el-input
          v-model="queryParams.fbaseqty"
          placeholder="请输入库存主单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存主单位名称" prop="fbaseqtyname">
        <el-input
          v-model="queryParams.fbaseqtyname"
          placeholder="请输入库存主单位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存量" prop="fqty">
        <el-input
          v-model="queryParams.fqty"
          placeholder="请输入库存量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="可用量" prop="favbqty">
        <el-input
          v-model="queryParams.favbqty"
          placeholder="请输入可用量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存状态" prop="fstockstatusid">
        <el-input
          v-model="queryParams.fstockstatusid"
          placeholder="请输入库存状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存状态名称" prop="fstockstatusname">
        <el-input
          v-model="queryParams.fstockstatusname"
          placeholder="请输入库存状态名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存组织" prop="fstockorgid">
        <el-input
          v-model="queryParams.fstockorgid"
          placeholder="请输入库存组织"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存组织名称" prop="fstockorgname">
        <el-input
          v-model="queryParams.fstockorgname"
          placeholder="请输入库存组织名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['k3:stock:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['k3:stock:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['k3:stock:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['k3:stock:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="stockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="库存组织名称" align="center" prop="id" />
      <el-table-column label="物料编码" align="center" prop="fmaterialid" />
      <el-table-column label="物料名称" align="center" prop="materialname" />
      <el-table-column label="规格型号" align="center" prop="model" />
      <el-table-column label="描述" align="center" prop="fUnBaseproperty6" />
      <el-table-column label="仓库编码" align="center" prop="fstockid" />
      <el-table-column label="仓库名称" align="center" prop="stockname" />
      <el-table-column label="批号" align="center" prop="flot" />
      <el-table-column label="生产日期" align="center" prop="fproducedate" />
      <el-table-column label="有效期至" align="center" prop="fexpirydate" />
      <el-table-column label="库存主单位" align="center" prop="fbaseqty" />
      <el-table-column label="库存主单位名称" align="center" prop="fbaseqtyname" />
      <el-table-column label="库存量" align="center" prop="fqty" />
      <el-table-column label="可用量" align="center" prop="favbqty" />
      <el-table-column label="库存状态" align="center" prop="fstockstatusid" />
      <el-table-column label="库存状态名称" align="center" prop="fstockstatusname" />
      <el-table-column label="库存组织" align="center" prop="fstockorgid" />
      <el-table-column label="库存组织名称" align="center" prop="fstockorgname" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['k3:stock:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['k3:stock:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料库存信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料编码" prop="fmaterialid">
          <el-input v-model="form.fmaterialid" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialname">
          <el-input v-model="form.materialname" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="规格型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="描述" prop="fUnBaseproperty6">
          <el-input v-model="form.fUnBaseproperty6" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="仓库编码" prop="fstockid">
          <el-input v-model="form.fstockid" placeholder="请输入仓库编码" />
        </el-form-item>
        <el-form-item label="仓库名称" prop="stockname">
          <el-input v-model="form.stockname" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="批号" prop="flot">
          <el-input v-model="form.flot" placeholder="请输入批号" />
        </el-form-item>
        <el-form-item label="生产日期" prop="fproducedate">
          <el-input v-model="form.fproducedate" placeholder="请输入生产日期" />
        </el-form-item>
        <el-form-item label="有效期至" prop="fexpirydate">
          <el-input v-model="form.fexpirydate" placeholder="请输入有效期至" />
        </el-form-item>
        <el-form-item label="库存主单位" prop="fbaseqty">
          <el-input v-model="form.fbaseqty" placeholder="请输入库存主单位" />
        </el-form-item>
        <el-form-item label="库存主单位名称" prop="fbaseqtyname">
          <el-input v-model="form.fbaseqtyname" placeholder="请输入库存主单位名称" />
        </el-form-item>
        <el-form-item label="库存量" prop="fqty">
          <el-input v-model="form.fqty" placeholder="请输入库存量" />
        </el-form-item>
        <el-form-item label="可用量" prop="favbqty">
          <el-input v-model="form.favbqty" placeholder="请输入可用量" />
        </el-form-item>
        <el-form-item label="库存状态" prop="fstockstatusid">
          <el-input v-model="form.fstockstatusid" placeholder="请输入库存状态" />
        </el-form-item>
        <el-form-item label="库存状态名称" prop="fstockstatusname">
          <el-input v-model="form.fstockstatusname" placeholder="请输入库存状态名称" />
        </el-form-item>
        <el-form-item label="库存组织" prop="fstockorgid">
          <el-input v-model="form.fstockorgid" placeholder="请输入库存组织" />
        </el-form-item>
        <el-form-item label="库存组织名称" prop="fstockorgname">
          <el-input v-model="form.fstockorgname" placeholder="请输入库存组织名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStock, getStock, delStock, addStock, updateStock, exportStock } from "@/api/k3/stock";

export default {
  name: "Stock",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 物料库存信息表格数据
      stockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fmaterialid: undefined,
        materialname: undefined,
        model: undefined,
        fUnBaseproperty6: undefined,
        fstockid: undefined,
        stockname: undefined,
        flot: undefined,
        fproducedate: undefined,
        fexpirydate: undefined,
        fbaseqty: undefined,
        fbaseqtyname: undefined,
        fqty: undefined,
        favbqty: undefined,
        fstockstatusid: undefined,
        fstockstatusname: undefined,
        fstockorgid: undefined,
        fstockorgname: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fmaterialid: [
          { required: true, message: "物料编码不能为空", trigger: "blur" }
        ],
        materialname: [
          { required: true, message: "物料名称不能为空", trigger: "blur" }
        ],
        fstockid: [
          { required: true, message: "仓库编码不能为空", trigger: "blur" }
        ],
        stockname: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" }
        ],
        flot: [
          { required: true, message: "批号不能为空", trigger: "blur" }
        ],
        fproducedate: [
          { required: true, message: "生产日期不能为空", trigger: "blur" }
        ],
        fqty: [
          { required: true, message: "库存量（主单位）不能为空", trigger: "blur" }
        ],
        favbqty: [
          { required: true, message: "可用量（主单位）不能为空", trigger: "blur" }
        ],
        fstockstatusid: [
          { required: true, message: "库存状态不能为空", trigger: "blur" }
        ],
        fstockorgid: [
          { required: true, message: "库存组织不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料库存信息列表 */
    getList() {
      this.loading = true;
      listStock(this.queryParams).then(response => {
        this.stockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fmaterialid: undefined,
        materialname: undefined,
        model: undefined,
        fUnBaseproperty6: undefined,
        fstockid: undefined,
        stockname: undefined,
        flot: undefined,
        fproducedate: undefined,
        fexpirydate: undefined,
        fbaseqty: undefined,
        fbaseqtyname: undefined,
        fqty: undefined,
        favbqty: undefined,
        fstockstatusid: undefined,
        fstockstatusname: undefined,
        fstockorgid: undefined,
        fstockorgname: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料库存信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStock(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料库存信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateStock(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addStock(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除物料库存信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStock(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有物料库存信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStock(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>